package com.puchi.sdkdemo.app.game.activity;

import android.os.Bundle;
import android.view.KeyEvent
import android.view.Window
import android.view.WindowManager
import com.bytedance.applog.AppLog
import com.orhanobut.logger.Logger

import com.zalyyh.mvvm.base.BaseActivity;

import com.puchi.sdkdemo.R;
import com.puchi.sdkdemo.BR;

import com.puchi.sdkdemo.databinding.ActivityGamesBinding;
import com.puchi.sdkdemo.app.game.model.GamesViewModel;
import com.puchi.sdkdemo.utils.H5interactive
import com.zalyyh.game.game.layaair.game.IMarket.IPlugin
import com.zalyyh.game.game.layaair.game.IMarket.IPluginRuntimeProxy
import com.zalyyh.game.game.layaair.game.Market.GameEngine


import demo.RuntimeProxy

import com.puchi.sdkdemo.utils.AllUtlis
import com.ss.android.common.applog.TeaAgent
import com.tencent.bugly.crashreport.CrashReport
import com.umeng.analytics.MobclickAgent
import com.umeng.analytics.game.UMGameAgent
import com.umeng.commonsdk.UMConfigure
import layaair.game.browser.ConchJNI


class GamesActivity : BaseActivity<ActivityGamesBinding, GamesViewModel>() {
    val AR_CHECK_UPDATE = 1
    private var mPlugin: IPlugin? = null
    private var mProxy: IPluginRuntimeProxy? = null
    var a = this
    internal var isLoad = false
    internal var isExit = false

    override fun initContentView(savedInstanceState: Bundle?): Int {
        //去掉窗口标题
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏顶部状态栏
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_games;
    }

    override fun initVariableId(): Int {
        return BR.viewModel;
    }

    override fun initParam() {
        super.initParam()
    }

    override fun initData() {
        super.initData();
        AppLog.setUserUniqueID(AppLog.getDid())
        MobclickAgent.onProfileSignIn(AppLog.getDid())
        model.setData(this, binding);
        AllUtlis.hideBottomUIMenu(this)
        H5interactive.get().activity = this
        H5interactive.get().callback = model.callback
        UMConfigure.setLogEnabled(true);// 设置输出运行时日志
        UMGameAgent.init(this); // U-Game子功能初始化
       initEngine()

    }

    fun initEngine() {
        mProxy = RuntimeProxy(this)
        mPlugin = GameEngine(this)
        mPlugin!!.game_plugin_set_runtime_proxy(mProxy)
        mPlugin!!.game_plugin_set_option("localize", "false")
        mPlugin!!.game_plugin_set_option(
            "gameUrl",
            "http://puchi-android-res.oss-cn-beijing.aliyuncs.com/awxxx/index.js"
        )//http://192.168.13.230:8900/bin/index.html
        mPlugin!!.game_plugin_init(3)
        val gameView = mPlugin!!.game_plugin_get_view()
        binding.game.removeAllViews()
        binding.game.addView(gameView)
        //this.setContentView(gameView)
        isLoad = true

    }

    override fun onPause() {
        super.onPause()
        TeaAgent.onPause(this)
//        MobclickAgent.onPause(this);
        UMGameAgent.onPause(this);
        if (model.mapH5Callback.size>0){
            ConchJNI.RunJS(model.mapH5Callback.get(2))
        }
        if (isLoad) mPlugin!!.game_plugin_onPause()
    }

    override fun onResume() {
        super.onResume()
        TeaAgent.onResume(this);
//        MobclickAgent.onResume(this);
        UMGameAgent.onResume(this)
        if (isLoad) mPlugin!!.game_plugin_onResume()
        if (model.mapH5Callback.size>0){
            ConchJNI.RunJS(model.mapH5Callback.get(1));
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        if (isLoad) mPlugin!!.game_plugin_onDestory()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        return super.onKeyDown(keyCode, event)
    }



}